import {mapGetters, mapMutations, mapActions} from 'vuex'
import {playMode} from 'common/js/config'
import {shuffle} from 'common/js/util'
import {getMusicList} from 'api/rank'
import {createSong} from 'common/js/song'

export const playlistMixin = {
  computed: {
    ...mapGetters([
      'playlist'
    ])
  },
  mounted() {
    this.handlePlaylist(this.playlist)
  },
  activated() {
    this.handlePlaylist(this.playlist)
  },
  watch: {
    playlist(newVal) {
      this.handlePlaylist(newVal)
    }
  },
  methods: {
    handlePlaylist() {
      throw new Error('component must implement handlePlaylist method')
    }
  }
}

export const playerMixin = { //player playlist
  computed: {
			iconMode(){//播放模式class
				// return this.mode===playMode.sequence?"icon-sequence":playMode.loop?"icon-loop":"icon-random" 原来写法
				if(this.mode==playMode.sequence){//0
					return "icon-sequence"
				}else if(this.mode==playMode.loop){//1
					return "icon-loop"
				}else{
					return "icon-random"
				}
			},
    ...mapGetters([
      'sequenceList',
      'playlist',
      'currentSong',
      'mode',
      'favoriteList',
			"currentIndex"
    ])
  },
  methods: {
    changeMode() {
      const mode = (this.mode + 1) % 3
			console.log("mode",mode)
      this.setPlayMode(mode)
      let list = null
      if (mode === playMode.random) {
        list = shuffle(this.sequenceList)
      } else {
        list = this.sequenceList
      }
      this.resetCurrentIndex(list)
      this.setPlaylist(list)
    },
    resetCurrentIndex(list) {
      let index = list.findIndex((item) => {
        return item.id === this.currentSong.id
      })
      this.setCurrentIndex(index)
    },
		//点击爱心
    toggleFavorite(song) {
      if (this.isFavorite(song)) {
        this.deleteFavoriteList(song)
      } else {
        this.saveFavoriteList(song)
      }
    },
    getFavoriteIcon(song) {
      if (this.isFavorite(song)) { //爱心
        return 'icon-favorite'
      }
      return 'icon-not-favorite'
    },
    isFavorite(song) { //是否有爱心
      const index = this.favoriteList.findIndex((item) => {
        return item.id === song.id
      })
      return index > -1
    },
    ...mapMutations({
      setPlayMode: 'SET_PLAY_MODE',
      setPlaylist: 'SET_PLAYLIST',
      setCurrentIndex: 'SET_CURRENT_INDEX',
      setPlayingState: 'SET_PLAYING_STATE'
    }),
    ...mapActions([
      'saveFavoriteList',
      'deleteFavoriteList'
    ])
  }
}

export const searchMixin = {
  data() {
    return {
      query: '',
      refreshDelay: 120
    }
  },
  computed: {
    ...mapGetters([
      'searchHistory'
    ])
  },
  methods: {
    onQueryChange(query) {//box组件传递的数据
      return this.query = query
    },
    blurInput() {//失去焦点
      this.$refs.searchBox.blur()
			this.$refs.shortcut.refresh()
    },
    addQuery(query) {
      this.$refs.searchBox.setQuery(query)
			console.log("querypppp",query)
    },
    saveSearch() {//搜索历史
      this.saveSearchHistory(this.query)
    },
    ...mapActions([
      'saveSearchHistory',
      'deleteSearchHistory'
    ])
  }
}

export const playpop = { //判断播放时机
  computed: {
    ...mapGetters([
      'dianji'
    ])
  },
  mounted() {
    let thta=this;
		function autoPlayMusic() {
				// 自动播放音乐效果，解决浏览器或者APP自动播放问题
				function musicInBrowserHandler() {
						musicPlay(true);
						document.body.removeEventListener('touchstart', musicInBrowserHandler);
				}
				document.body.addEventListener('touchstart', musicInBrowserHandler);
				// 自动播放音乐效果，解决微信自动播放问题
				function musicInWeixinHandler() {
						musicPlay(true);
						document.addEventListener("WeixinJSBridgeReady", function () {
								musicPlay(true);
						}, false);
						document.removeEventListener('DOMContentLoaded', musicInWeixinHandler);
				}
				document.addEventListener('DOMContentLoaded', musicInWeixinHandler);
			}
		function musicPlay(isPlay) {
					var audio = document.getElementById('music_mp3_0');
					if (isPlay && audio.paused) {
							audio.play();
					}
					if (!isPlay && !audio.paused) {
							audio.pause();
					}
			}
    if(thta.dianji>0){ //判断播放时机
    		autoPlayMusic();
				console.log("psssseeeeees")
    }else{
    	thta.pcoc(1)
			console.log("p789")
			function musicIn() {
				let musicEle0 = document.getElementById('music_mp3_0');
						musicEle0.play();
						document.body.removeEventListener('touchstart', musicIn);
				}
				document.body.addEventListener('touchstart', musicIn);
				console.log("pppsssss")
		}
  },
	methods: {
		 ...mapMutations({
			  pcoc:'pcoc' //点击进入页面
			})
	 }
  
}

export const RenTop = { //获取热门信息
  data() {
    return {
      songs: [],
			dapk:[] //查看是否有点击缓存数据
    }
  },
	created() {
			this._getMusicList()
			this.dapk=this.topList
			console.log("this.topList",this.topList)
  },
	computed: {
	  ...mapGetters([
	    'topList'
	  ])
	},
  methods: {
   		_getMusicList() { //获取热门排行的数据 26 
			  let sID=this.topList.id?this.topList.id:"26"
				getMusicList(sID).then((res) => {
			    if (res.code === 0) {
							this.songs = this._normalizeSongs(res.songlist)
			    }
			  })
			},
			_normalizeSongs(list) {
			  let ret = []
			  list.forEach((item) => {
			    const musicData = item.data
			    if (musicData.songid && musicData.albummid) {
			        ret.push(createSong(musicData))
			    }
			  })
				ret.forEach((item) =>{//增加一个属性
					if(!item.pic){item.pic=item.image}
				})
			  return ret
			},
    ...mapActions([
      'saveSearchHistory',
      'deleteSearchHistory'
    ])
  }
}
